//
//  TIMFriendCell.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/6/30.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMFriendCell.h"
#import "TIMMessageCountLabel.h"
#import "TIMDemoMacro.h"

#import "TIMStorageManager.h"

@interface TIMFriendCell ()

@property (nonatomic,strong) UILabel *nameLabel;//!<姓名
@property (nonatomic, strong) UIImageView *headImageView;// 头像
@property (nonatomic, strong) TIMMessageCountLabel *messageLabel;// 消息数

@end

@implementation TIMFriendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //布局View
        //        self.contentView.backgroundColor = [UIColor redColor];
        [self setUpView];
    }
    return self;
}

- (void)setUpView{
    //头像
    [self.contentView addSubview:self.headImageView];
    //姓名
    [self.contentView addSubview:self.nameLabel];
    //计数
    [self.contentView addSubview:self.messageLabel];
}

- (void)setUser:(TIMUserInfo *)user {
    if (user.avatarURL) {
        [self.headImageView sd_setImageWithURL:[user.avatarURL filterChineseAndThin] placeholderImage:[UIImage imageNamed:@"tab_mine_unselect"]];
    } else {
        [self.headImageView setImage:[UIImage imageNamed:@"tab_mine_unselect"]];
    }
    
    [self.nameLabel setText:user.showName];
}

- (void)fillData:(NSIndexPath *)index unRead:(NSInteger)unRead {
    if (index.row == 0 && index.section == 0 && unRead > 0) {
        _messageLabel.hidden = NO;
        _messageLabel.messageCount = unRead;
    } else {
        _messageLabel.hidden = YES;
    }
}
- (void)hiddenMessageLabel {
    _messageLabel.hidden = YES;
}

#pragma mark - Get
- (UIImageView *)headImageView{
    if (!_headImageView) {
        _headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 32, 32)];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = _headImageView.frame.size.height/2;
        _headImageView.contentMode = UIViewContentModeScaleAspectFill;
        
        [_headImageView setContentMode:UIViewContentModeScaleAspectFill];
    }
    return _headImageView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(62, 0, Screen_Width-62, 52)];
        [_nameLabel setFont:TPSFontSize(14)];
    }
    return _nameLabel;
}

- (TIMMessageCountLabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [[TIMMessageCountLabel alloc] initWithFrame:CGRectMake(Screen_Width - 20 - 16, self.headImageView.center.y - 16/2, 16, 16)];
        _messageLabel.layer.cornerRadius = 16/2;
        _messageLabel.layer.masksToBounds = YES;
    }
    return _messageLabel;
}

@end
